package cn.tedu.tmall.front.mall.controller;

import cn.tedu.tmall.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.tmall.common.pojo.vo.PageData;
import cn.tedu.tmall.common.web.JsonResult;
import cn.tedu.tmall.front.mall.pojo.param.CommentAddNewParam;
import cn.tedu.tmall.front.mall.pojo.vo.CommentListItemVO;
import cn.tedu.tmall.front.mall.service.ICommentService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

/**
 * 处理评论相关请求的控制器类
 *
 * @author java@tedu.cn
 * @version 3.0
 */
@Slf4j
@RestController
@RequestMapping("/comments")
@Validated
@Api(tags = "3. 评论管理")
public class CommentController {

    @Autowired
    private ICommentService commentService;

    public CommentController() {
        log.debug("创建控制器类对象：CommentController");
    }

    @PostMapping("/add-new/goods-comment")
    @ApiOperation("发表商品评论")
    @ApiOperationSupport(order = 100)
    public JsonResult addNewArticleComment(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                                           @Valid CommentAddNewParam commentAddNewParam) {
        log.debug("开始处理【发表商品评论】的请求，参数：{}", commentAddNewParam);
        commentService.addNew(currentPrincipal, commentAddNewParam);
        return JsonResult.ok();
    }

    @GetMapping("/list-by-goods")
    @ApiOperation("查询商品的评论列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", value = "商品ID", required = true, paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult listByGoods(@Range(min = 1, message = "请提交有效的商品ID！") Long goodsId,
                                  @Range(min = 1, message = "请提交有效的页码值！") Integer page) {
        log.debug("开始处理【查询商品的评论列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<CommentListItemVO> pageData = commentService.list(goodsId, pageNum);
        return JsonResult.ok(pageData);
    }

}
